<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/13
 * Time: 15:39
 */

namespace Admin\Controller;


class CooperationController extends CommenController{
    /**
     * 广告服务商数据统计
     */
    public function ad_statis(){
        $info=M('market_reg_statis',null,'XYC_SERVICE');
        $where=MycommonController::set_where([],[],'ctime');
        $where2['mrs.shop_identifier']=$_SESSION['shop_identifier'];
        $sql=$info->alias('mrs')->where($where2)->group('mrs.chan_id,mrs.server_id')->field('mrs.chan_id,mrs.server_id')->select(false);
        $tol = $info->table('('.$sql.') a')->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $data=$info->join('mrs left join xyc.channel c on c.chan_id=mrs.chan_id')->join('left join xyc_service.service_market sm on sm.id=mrs.server_id')->LIMIT($page->listfirst, $page->listRows)
            ->where($where2)->group('mrs.chan_id,mrs.server_id')->field('mrs.server_id,mrs.chan_id,sm.name,c.channel_name')->select();
        $chan_id=array_column($data,'chan_id');
        $server_id=array_column($data,'server_id');
        $browse=M('browse',null,'XYC')->where(['channel_num'=>['in',$chan_id],'server_id'=>['in',$server_id],'dtime'=>['between',[I("start"),I("end")]]])->group('channel_num,server_id')->field('channel_num,server_id,sum(num) as num')->select();
        $where['chan_id']=['in',$chan_id];
        $where['server_id']=['in',$server_id];
        $borrow=M('market_borrow_statis',null,'XYC_SERVICE')->where($where)->group('chan_id,server_id')->field('chan_id,server_id,sum(borrow_user) as borrow_user,sum(borrow_money) as borrow_money')->select();
        $cert=M('market_cert_statis',null,'XYC_SERVICE')->where($where)->group('chan_id,server_id')->field('chan_id,server_id,sum(cert) as cert')->select();
        $cert_via=M('market_cert_via_statis',null,'XYC_SERVICE')->where($where)->group('chan_id,server_id')->field('chan_id,server_id,sum(cert_via) as cert_via')->select();
        $reg=M('market_reg_statis',null,'XYC_SERVICE')->where($where)->group('chan_id,server_id')->field('chan_id,server_id,sum(reg_num) as reg_num')->select();
        foreach ($data as $k=>$v){
            foreach ($browse as $k1=>$v1){
                if($v1['channel_num']==$v['chan_id'] && $v1['server_id']==$v['server_id']){
                    $data[$k]['browse']=$v1['num'];
                    break;
                }
            }
            $data[$k]['browse']=empty($data[$k]['browse'])?0:$data[$k]['browse'];
            foreach($borrow as $k2=>$v2){
                if($v2['chan_id']==$v['chan_id'] && $v2['server_id']==$v['server_id']){
                    $data[$k]['borrow_user']=$v2['borrow_user'];
                    $data[$k]['borrow_money']=$v2['borrow_money'];
                    break;
                }
            }
            $data[$k]['borrow_user']=empty($data[$k]['borrow_user'])?0:$data[$k]['borrow_user'];
            $data[$k]['borrow_money']=empty($data[$k]['borrow_money'])?0:$data[$k]['borrow_money'];
            foreach($cert as $k3=>$v3){
                if($v3['chan_id']==$v['chan_id'] && $v3['server_id']==$v['server_id']){
                    $data[$k]['cert']=$v3['cert'];
                    break;
                }
            }
            $data[$k]['cert']=empty($data[$k]['cert'])?0:$data[$k]['cert'];
            foreach($cert_via as $k4=>$v4){
                if($v4['chan_id']==$v['chan_id'] && $v4['server_id']==$v['server_id']){
                    $data[$k]['cert_via']=$v4['cert_via'];
                    break;
                }
            }
            $data[$k]['cert_via']=empty($data[$k]['cert_via'])?0:$data[$k]['cert_via'];

            foreach($reg as $k4=>$v5){
                if($v5['chan_id']==$v['chan_id'] && $v5['server_id']==$v['server_id']){
                    $data[$k]['reg_num']=$v5['reg_num'];
                    break;
                }
            }
            $data[$k]['reg_num']=empty($data[$k]['reg_num'])?0:$data[$k]['reg_num'];
        }
        $this->assign(array(
            'list' => $data,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->display();
    }
}